//
//  Service.m
//  Listen Up
//
//  Created by Denis Demjanko on 01.06.12.
//  Copyright (c) 2012 it-dimension. All rights reserved.
//

#import "Service.h"
#import "UIDevice+IdentifierAddition.h"
#import "UserManager.h"

@implementation Service

+ (Service*)sharedManeger{
    __strong static Service* _sharedManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedManager = [[Service alloc]init];
        });
    return _sharedManager;
}

-(void)getNewFacts:(int)fromFact_id withHandler:(RMResult)handler{
    NSMutableString *postBody = [NSMutableString string];
    [postBody appendFormat:@"fact_id=%d",fromFact_id];
    
    NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://211754.facts.web.hosting-test.net/get_facts.php"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
    [req setHTTPMethod:@"POST"];
    [req setHTTPBody:[postBody dataUsingEncoding:NSUTF8StringEncoding]];
    
    [self sendHiddenRequest:req withCompletionHandler:handler];
}

-(void)getFactInfo:(int)fact_id withHandler:(RMResult)handler{
    NSMutableString *postBody = [NSMutableString string];
    [postBody appendFormat:@"fact_id=%d",fact_id];    
    [postBody appendFormat:@"&device=%@",[[UIDevice currentDevice] uniqueGlobalDeviceIdentifier]];    

    NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://211754.facts.web.hosting-test.net/get_fact_info.php"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
    [req setHTTPMethod:@"POST"];
    [req setHTTPBody:[postBody dataUsingEncoding:NSUTF8StringEncoding]];
    
    [self sendHiddenRequest:req withCompletionHandler:handler];
}

-(void)addLike:(int)fact_id isLike:(int)isLike withHandler:(RMResult)handler{
    NSMutableString *postBody = [NSMutableString string];
    [postBody appendFormat:@"fact_id=%d",fact_id];    
    [postBody appendFormat:@"&device=%@",[[UIDevice currentDevice] uniqueGlobalDeviceIdentifier]];    
    [postBody appendFormat:@"&is_like=%d",isLike];    

    NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://211754.facts.web.hosting-test.net/add_like.php"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
    [req setHTTPMethod:@"POST"];
    [req setHTTPBody:[postBody dataUsingEncoding:NSUTF8StringEncoding]];
    
    [self sendHiddenRequest:req withCompletionHandler:handler];
}

-(void)sendComment:(int)fact_id comment:(NSString*)comment withHandler:(RMResult)handler{
    NSDateFormatter *form = [[NSDateFormatter alloc] init];
    [form setDateFormat:@"dd/MM/yyyy"];
    
    NSString *date = [form stringFromDate:[NSDate date]];
    
    NSMutableString *postBody = [NSMutableString string];
    [postBody appendFormat:@"fact_id=%d",fact_id];    
    [postBody appendFormat:@"&comment=%@",comment];    
    [postBody appendFormat:@"&user_name=%@",[[UserManager currentUser] name]];    
    [postBody appendFormat:@"&user_photo=%@",[[UserManager currentUser] ava]];    
    [postBody appendFormat:@"&date=%@",date];    


    NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://211754.facts.web.hosting-test.net/add_comment.php"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
    [req setHTTPMethod:@"POST"];
    [req setHTTPBody:[postBody dataUsingEncoding:NSUTF8StringEncoding]];
    
    [self sendHiddenRequest:req withCompletionHandler:handler];
}

-(void)getComments:(int)fact_id withHandler:(RMResult)handler{
    NSMutableString *postBody = [NSMutableString string];
    [postBody appendFormat:@"fact_id=%d",fact_id];    
    
    NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://211754.facts.web.hosting-test.net/get_comments.php"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
    [req setHTTPMethod:@"POST"];
    [req setHTTPBody:[postBody dataUsingEncoding:NSUTF8StringEncoding]];
    
    [self sendHiddenRequest:req withCompletionHandler:handler];
}

-(void)getTopFacts:(RMResult)handler{
    NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://211754.facts.web.hosting-test.net/get_top.php"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
    
    [self sendHiddenRequest:req withCompletionHandler:handler];
}

@end
